package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.prometheus.client.Summary;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;
	@Resource
	private VodService vodService;

	/**
	 * 批量获取下单付费声音列表
	 * @param trackId
	 * @param trackCount
	 * @return
	 */
	@TsLogin
	@Operation(summary = "批量获取下单付费声音列表")
	@GetMapping("findPaidTrackInfoList/{trackId}/{trackCount}")
	Result findPaidTrackInfoList(Long trackId, Integer trackCount){
		//获取userId
		Long userId = AuthContextHolder.getUserId();
		//调用服务
		List<TrackInfo> list=trackInfoService.findPaidTrackInfoList(trackId, trackCount,userId);
		//返回结果
		return Result.ok(list);
	}

	/**
	 * 根据声音ID，获取声音分集列表
	 * @param trackId
	 * @return
	 */
	@TsLogin
	@Operation(summary = "获取声音分集列表")
	@GetMapping("findUserTrackPaidList/{trackId}")
	public Result findUserTrackPaidList(@PathVariable("trackId") Long trackId) {
		//获取userId
		Long userId = AuthContextHolder.getUserId();
		//调用服务
		List<Map<String, Object>> list = trackInfoService.findUserTrackPaidList(trackId, userId);
		//返回结果
		return Result.ok(list);
	}

	/**
	 * 根据专辑Id获取声音列表
	 * @param albumId
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@TsLogin(requireLogin = false)
	@Operation(summary = "根据专辑Id获取声音列表")
	@GetMapping("/findAlbumTrackPage/{albumId}/{pageNo}/{pageSize}")
	public Result findAlbumTrackPage(@PathVariable("albumId") Long albumId,
									 @PathVariable("pageNo") Long pageNo,
									 @PathVariable("pageSize") Long pageSize){
		//获取userId
		Long userId = AuthContextHolder.getUserId();
		//获取分页数据，Page属于baomidou
		Page<AlbumTrackListVo> page = new Page<>(pageNo, pageSize);
		//调用服务
		IPage<AlbumTrackListVo> iPage = trackInfoService.findAlbumTrackPage(page, albumId, userId);
		//调用服务
		return Result.ok(iPage);
	}
	@Operation(summary = "修改声音")
	@PutMapping("/updateTrackInfo/{trackId}")
	public Result updateTrackInfo(@PathVariable("trackId") Long trackId,
								  @RequestBody TrackInfoVo trackInfoVo){
		trackInfoService.updateTrackInfo(trackId,trackInfoVo);
		return Result.ok();
	}

	@Operation(summary = "回显声音")
	@GetMapping("/getTrackInfo/{trackId}")
	public Result getTrackInfo(@PathVariable("trackId") Long trackId) {
		TrackInfo trackInfo = trackInfoService.getById(trackId);
		return Result.ok(trackInfo);
	}
	/**
	 * 删除声音
	 * @param trackId
	 * @return
	 */
	@Operation(summary = "删除声音")
	@DeleteMapping("/removeTrackInfo/{trackId}")
	public Result removeTrackInfo(@PathVariable Long trackId) {
		//删除声音
		trackInfoService.removeTrackInfo(trackId);
		return Result.ok();
	}

	/**
	 * 获取用户声音分页列表
	 * @param pageNo
	 * @param pageSize
	 * @param trackInfoQuery
	 * @return
	 */
	@Operation(summary="获取用户声音分页列表")
	@PostMapping("/findUserTrackPage/{pageNo}/{pageSize}")
	public Result findUserTrackPage(@PathVariable("pageNo") Long pageNo,
									@PathVariable("pageSize") Long pageSize,
									@RequestBody TrackInfoQuery trackInfoQuery){
		//获取用户ID
		long userId = AuthContextHolder.getUserId() == null ? 1l : AuthContextHolder.getUserId();
		//生成分页对象
		Page<TrackListVo> trackInfoPage = new Page<>(pageNo, pageSize);
		trackInfoQuery.setUserId(userId);
		//查找分页数据
		trackInfoService.findUserTrackPage(trackInfoPage,trackInfoQuery);
		//返回结果
		return Result.ok(trackInfoPage);
	}


	/**
	 * 添加声音
	 * @param trackInfoVo
	 * @return
	 */
	@Operation(summary="添加声音")
	@PostMapping("/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo){
		//获取用户ID
		long userId = AuthContextHolder.getUserId() == null ? 1l : AuthContextHolder.getUserId();
		//保存声音
		trackInfoService.saveTrackInfo(trackInfoVo,userId);

		return Result.ok();
	}

	/**
	 * 上传声音
	 * @param file
	 * @return
	 */
	@Operation(summary="上传声音")
	@PostMapping("/uploadTrack")
	public Result uploadTrack(MultipartFile file) {
		//上传文件-》腾讯云点播vod ->返回文件id,和文件URL
		Map<String ,Object> map=vodService.uploadTrack(file);
		return Result.ok(map);
	}




}

